package com.bajiao.app.config.redis;

import cn.hutool.core.util.ObjectUtil;
import com.bajiao.common.constant.CommonConstant;
import com.bajiao.common.constant._Whole;
import com.bajiao.entity.order.CouponOrder;
import com.bajiao.entity.order.Order;
import com.bajiao.entity.recharge.Recharge;
import com.bajiao.entity.user.UserCoupon;
import com.bajiao.service.order.CouponOrderService;
import com.bajiao.service.order.OrderService;
import com.bajiao.service.recharge.RechargeService;
import com.bajiao.service.user.UserCouponService;
import com.bajiao.service.user.UserService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;

public class KeyExpiredListener extends KeyExpirationEventMessageListener {

    @Resource
    private UserCouponService userCouponService;
    @Resource
    private RechargeService rechargeService;
    @Resource
    private OrderService orderService;
    @Resource
    private CouponOrderService couponOrderService;

    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * redis监听key过期
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 获取已过期的key
        String key = message.toString();
        //@create by FanJq 2024/12/19 10:29 优惠券到期事件
        if (key.contains(_Whole.couponExpiredKey)) {
            System.out.println(String.format("execute 优惠券到期事件 凭证：%s", key));
            String[] keys = key.split(StringPool.COLON);
            UserCoupon userCoupon = userCouponService.getById(keys[_Whole.ONE]);
            if (ObjectUtil.isNotNull(userCoupon) && userCoupon.getStatus() == _Whole.ONE) {
                //@create by FanJq 2024/12/19 10:30 执行优惠券到期事件
                userCouponService.expiredEvent(userCoupon);
            }
        }
        //@create by FanJq 2024/12/25 11:34 充值订单失效事件
        if (key.contains(_Whole.rechargeOrderExpiredKey)) {
            System.out.println(String.format("execute 充值订单失效事件 凭证：%s", key));
            String[] keys = key.split(StringPool.COLON);
            Recharge recharge = rechargeService.getById(keys[_Whole.ONE]);
            if (ObjectUtil.isNotNull(recharge) && recharge.getOrderStatus() == _Whole.ONE) {
                //@create by FanJq 2024/12/25 11:35 充值订单失效事件
                rechargeService.expiredEvent(recharge);
            }
        }

        //@create by FanJq 2025/2/19 18:59 订单失效事件
        if (key.contains(_Whole.orderExpiredKey)) {
            System.out.println(String.format("execute 订单失效事件 凭证：%s", key));
            String[] keys = key.split(StringPool.COLON);
            Order order = orderService.getById(keys[_Whole.ONE]);
            if (ObjectUtil.isNotNull(order) && order.getStatus() == _Whole.ZERO) {
                //@create by FanJq 2025/2/22 11:30 订单失效事件
                orderService.expiredEvent(order);
            }
        }

        //@create by FanJq 2025/2/19 18:59 优惠券订单失效事件
        if (key.contains(_Whole.couponExpiredKey)) {
            System.out.println(String.format("execute 优惠券订单失效事件 凭证：%s", key));
            String[] keys = key.split(StringPool.COLON);
            CouponOrder couponOrder = couponOrderService.getById(keys[_Whole.ONE]);
            if (ObjectUtil.isNotNull(couponOrder) && couponOrder.getStatus() == _Whole.ZERO) {
                //@create by FanJq 2025/2/22 11:30 优惠券订单失效事件
                couponOrderService.expiredEvent(couponOrder);
            }
        }
    }
}
